<?php

use Illuminate\Support\Facades\Redis;

if (!function_exists('includeRouteFiles')) {

    /**
     * Loops through a folder and requires all PHP files
     * Searches sub-directories as well.
     *
     * @param $folder
     */
    function includeRouteFiles($folder)
    {
        $directory = $folder;
        $handle = opendir($directory);
        $directory_list = [$directory];

        while (false !== ($filename = readdir($handle))) {
            if ($filename != '.' && $filename != '..' && is_dir($directory . $filename)) {
                array_push($directory_list, $directory . $filename . '/');
            }
        }

        foreach ($directory_list as $directory) {
            foreach (glob($directory . '*.php') as $filename) {
                require $filename;
            }
        }
    }
}

if (!function_exists('get_request_page')) {
    function get_request_page()
    {
        return intval(request()->get('per_page', 0));
    }
}

if (!function_exists('dingo_route')) {
    /**
     * 根据别名获得url.
     *
     * @param string $version
     * @param string $name
     * @param array $params
     * @return string
     */
    function dingo_route($version, $name, $params = [])
    {
        return app(\Dingo\Api\Routing\UrlGenerator::class)->version($version)->route($name, $params);
    }
}

if (!function_exists('generate_top_url')) {
    function generate_top_url($path = '/', $prefix = null, $param = '')
    {
        $url = env('URL_SUBSTATION');
        $url = $prefix ? $prefix . '.' . $url . $path : $url . $path;
        if ($param) {
            $url .= '/' . $param;
        }
        return 'http://' . $url;
    }
}

if (!function_exists('generate_is_top_url')) {
    function generate_is_top_url($path = '/', $prefix = null, $param = '')
    {
        $url = env('URL_SUBSTATION');
        $initials = null;
        if ($prefix !== null) {
            $initials = $prefix->is_top ? null : $prefix->region->initials;
        }
        $url = $initials ? $initials . '.' . $url . $path : $url . $path;
        if ($param) {
            $url .= '/' . $param;
        }
        return 'http://' . $url;
    }
}

if (!function_exists('is_domain_top')) {
    function is_domain_top()
    {
        return in_array(request()->getHost(), config('urls.prefix'));
    }
}